package com.lw.netty.websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.ImmediateEventExecutor;

import java.net.InetSocketAddress;

/**
 * 引导
 * @author lenovo
 *
 */
public class ChatServer {
	//用来保存所有已经连接的WebSocket Channel
	private final ChannelGroup channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);
	private final EventLoopGroup group = new NioEventLoopGroup();
	private Channel channel;
	
	/**
	 * 引导服务器
	 * @param address
	 * @return
	 */
	public ChannelFuture start(InetSocketAddress address){
		ServerBootstrap bootstrap = new ServerBootstrap();
		bootstrap.group(group).channel(NioServerSocketChannel.class)
		.childHandler(createInitializer(channelGroup));
		ChannelFuture future = bootstrap.bind(address);
		future.syncUninterruptibly();
		channel = future.channel();
		return future;
	}
	
	protected ChannelInitializer<Channel> createInitializer(ChannelGroup group){
		return new ChatServerInitializer(group);
	}
	
	/**
	 * 关闭服务器，并释放所有资源
	 */
	public void destroy(){
		if(channel!=null){
			channel.close();
		}
		channelGroup.close();
		group.shutdownGracefully();
	}
	
//	public static void main(String[] args) {
//		if(args.length!=1){
//			System.err.println("Please give port as argument.");
//			System.exit(1);//异常退出
//		}
//		int port = Integer.parseInt(args[0]);
//		final ChatServer endpoint = new ChatServer();
//		ChannelFuture future = endpoint.start(new InetSocketAddress(port));
//		Runtime.getRuntime().addShutdownHook(new Thread(){
//			@Override
//			public void run(){
//				endpoint.destroy();
//			}
//		});
//		future.channel().closeFuture().syncUninterruptibly();
//	}
}
